package com.manor.main.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.manor.common.utils.DpUtil
import com.manor.common.utils.RecyclerDivider
import com.manor.main.R
import com.manor.main.bean.HomeTxtBean
import kotlinx.android.synthetic.main.popup_home_say.view.*

/**
 *


 * @date  2020/5/22  17:46
 * @version 1.0
 */
class HomeSayPopup(context: Context, private val dataList: MutableList<HomeTxtBean>, val listener: OnSelectListener)
    : AttachPopupView(context) {

    private var mAdapter :MAdapter? = null


    override fun getImplLayoutId(): Int {
        return R.layout.popup_home_say
    }

    override fun onCreate() {
        super.onCreate()
        mAdapter = MAdapter(R.layout.item_home_say, dataList)
        mRecycler.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context, androidx.recyclerview.widget.LinearLayoutManager.VERTICAL, false)
        mRecycler.addItemDecoration(RecyclerDivider(context, LinearLayoutManager.VERTICAL).apply {
            this.setHorizontaloffset(DpUtil.dp2px(16),-DpUtil.dp2px(16))
        })
        mRecycler.adapter = mAdapter
        mAdapter?.run {
            this.setOnItemClickListener { _, _, position ->
                listener.onSelect(position,dataList[position].title)
                dismiss()
            }
        }
    }

//    override fun getPopupBackground(): Drawable {
//        return DrawableCreator.Builder()
//                .setCornersRadius(DpUtil.dp2px(12).toFloat())
//                .setSolidColor(Color.parseColor("#ffffff"))
//                .build()
//    }

    private class MAdapter(layout: Int, data: MutableList<HomeTxtBean>) : BaseQuickAdapter<HomeTxtBean, BaseViewHolder>(layout,
            data){
        override fun convert(helper: BaseViewHolder, item: HomeTxtBean) {
             item.run {
                helper.setText(R.id.tvText, this.title)
             }
        }

    }

}
